package com.tanhua.sso.controller;


import com.tanhua.sso.pojo.Manage;
import com.tanhua.sso.service.LoginService;
import com.tanhua.sso.utils.ManageThreadLocal;
import com.tanhua.sso.utils.NoAuthorization;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/management/system/users")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @NoAuthorization
    public ResponseEntity<Map<String,String>> login(@RequestBody Map<String,String> param){

        String username = param.get("username");
        String password = param.get("password");
        String verificationCode = param.get("verificationCode");
        String uuid = param.get("uuid");
        Map map = new HashMap();
        try {
            String token = loginService.login(username,password,verificationCode,uuid);
            if (StringUtils.isNotEmpty(token)){
                map.put("token",token);
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);


    }

    /**
     * 获取信息
     *
     * @return
     */
    @PostMapping("/profile")
    public ResponseEntity<Map> profile(@RequestHeader("Authorization") String token){

        String[] tokenStr = token.split(" ");
        token = tokenStr[1];
        try {
            Manage manage = loginService.queryToken(token);
            if (manage != null){
                Map<String, String> map = loginService.profile(manage);
                if (!CollectionUtils.isEmpty(map)) {
                    return ResponseEntity.ok(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

    /**
     * 登出
     * @param token
     * @return
     */
    @PostMapping("logout")
    public ResponseEntity<Void> logout(@RequestHeader("Authorization") String token){
        ManageThreadLocal.remove();
        return ResponseEntity.ok().build();
    }

    /**
     * 获取验证码
     * @param uuid
     * @return
     */
    @GetMapping("/verification")
    public ResponseEntity<Void> verification(@RequestParam String uuid, HttpServletRequest request, HttpServletResponse response){

        try {
            boolean result = loginService.getVerification(uuid,request,response);
            if (result){
                return ResponseEntity.ok().build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public Manage queryUserByToken(@PathVariable("token") String token) {
        return this.loginService.queryToken(token);
    }
}
